/*
 * Copyright (c) @ justbk. 2021-2031. All rights reserved.
 */
package org.opengauss;

import org.opengauss.driver.DriverInfoManager;
import org.opengauss.driver.IDriverInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Title: the IssueMain class.
 * <p>
 * Description:
 *
 * @author justbk
 * @version [issueManager 0.0.1, 2022/11/10]
 * @since 2022/11/10
 */
public class IssueMain {
    public static void main(String ... args) throws SQLException {
        String key = "db2";
        IDriverInfo info = DriverInfoManager.getInfo(key);
        String sql = "select count(*) from pg_class";
        try (Connection conn = DriverInfoManager.getConnection(info)) {
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.execute();
            }
            Statement stmt = conn.createStatement();
            int rc = stmt.executeUpdate("CREATE CLIENT MASTER KEY ImgCMK1 WITH ( KEY_STORE = localkms, KEY_PATH = \"key_path_value\" , ALGORITHM = RSA_2048)");
            int rc2 = stmt.executeUpdate("CREATE COLUMN ENCRYPTION KEY ImgCEK1 WITH VALUES (CLIENT_MASTER_KEY = ImgCMK1, ALGORITHM  = AEAD_AES_256_CBC_HMAC_SHA256);");
            int rc3 = stmt.executeUpdate("CREATE TABLE creditcard_info (id_number    int, name  varchar(50) encrypted with (column_encryption_key = ImgCEK1," +
                    " encryption_type = DETERMINISTIC)," +
                    "credit_card  varchar(19) encrypted with (column_encryption_key = ImgCEK1, encryption_type = DETERMINISTIC));");
            // 插入数据
            int rc4 = stmt.executeUpdate("INSERT INTO creditcard_info VALUES (1,'joe','6217986500001288393');");
            // 查询加密表
            ResultSet rs = null;
            rs = stmt.executeQuery("select * from creditcard_info where name = 'joe';");
            // 关闭语句对象
            stmt.close();
        }
    }
}
